<?php
require_once APP . '/application/controllers/FrontAction.php';
require_once APP . '/application/models/MeetingModel.php';

/**
 * 首页
 */
class IndexController extends FrontAction
{
    /**
     * 本controller通用模块
     */
    public function init ()
    {
    }

    /**
     * 首页
     *
     */
    public function indexAction()
    {
		
    }
    
    /**
     * 注册
     *
     */
    public function regAction()
    {
    	global $cfg;
    	$mid = (int)$this->getParam("mid");
    	$lang = $this->getParam("lang");
    	$code = $this->getParam("code");
    	$mail = $this->getParam("mail");
    	$show = $this->getParam("show");
    	
    	if ($lang == 'en')
    		$cfg['lang'] = 'en';
    	else 
    		$cfg['lang'] = 'cn';
    	
    	$obj_meet = new MeetingModel($this->db);
    	$arr_info = $obj_meet->show($mid);
    	
    	if (empty($arr_info)){
    		exit(_T("参数错误"));
    	}
    	
    	// 检索是否已注册
    	$mailinfo = $this->db->fetchRow("select email,code,isreg from email where mid=? and email='?'", array($mid, $mail));
    	if (empty($mailinfo) && empty($show)){
    		exit(_T("数据不存在"));
    	}
    	if(($mailinfo['email'] != $mail || $mailinfo['code'] != $code ) && empty($show)){
    		exit(_T("注册码错误"));
    	}
    	    	
    	// 读取字段设置
    	$arr_dict = $obj_meet->fetchAllDict($mid);
    	
    	$arr_display_dict_pre = array();
    	$arr_display_dict_after = array();
    	$arr_display_dict = array();
    	
    	$index = 0;
    	foreach ($arr_dict as $row){
    		if ($row['isoneline'] && isset($arr_display_dict[$index]))
    			$index++;
    		
    		$arr_display_dict[$index][] = $row;
    		
    		if (count($arr_display_dict[$index]) == 3 || $row['isoneline'])
    			$index++;
    		
    		if ($row['did'] == 10){
    			$arr_display_dict_pre = $arr_display_dict;
    			$arr_display_dict = array();
    			$index = 0;
    		}
    	}
    	if (!empty($arr_display_dict_pre))
    		$arr_display_dict_after = $arr_display_dict;
    	    	
    	$this->view->meetinfo = $arr_info;
    	$this->view->dictinfopre = $arr_display_dict_pre;
    	$this->view->dictinfoafter = $arr_display_dict_after;
    	
    	$this->view->isreg = $mailinfo['isreg'];
    	$this->view->show = $show;// 是否为预览模式
    	$this->view->lang = $lang;
    	$this->view->mail = $mail;
    	$this->view->code = $code;
    	
    	$this->render("index");
    }
    
    /**
     * 注册
     *
     */
    public function doregAction()
    {
    	global $cfg;
    	$mid = (int)$this->getParam("mid");
    	$lang = $this->getParam("lang");
    	$code = $this->getParam("code");
    	$mail = $this->getParam("mail");
    	$show = $this->getParam("show");
    	
    	if ($lang == 'en')
    		$cfg['lang'] = 'en';
    	else 
    		$cfg['lang'] = 'cn';
    	
    	$obj_meet = new MeetingModel($this->db);
    	$arr_info = $obj_meet->show($mid);
    	
    	if (empty($arr_info)){
    		exit(_T("参数错误"));
    	}
    	
    	// 检索是否已注册
    	$mailinfo = $this->db->fetchRow("select email,code,isreg from email where mid=? and email='?'", array($mid, $mail));
    	if (empty($mailinfo) && empty($show)){
    		exit(_T("数据不存在"));
    	}
    	if(($mailinfo['email'] != $mail || $mailinfo['code'] != $code )){
    		exit(_T("注册码错误"));
    	}
    	
    	if ($mailinfo['isreg']){
    		exit(_T("邮箱已注册"));
    	}
    	
    	// 读取字段设置
    	$arr_dict = $obj_meet->fetchAllDict($mid);
    	
    	$arr_display_dict_pre = array();
    	$arr_display_dict_after = array();
    	$arr_display_dict = array();
    	$row_10 = array();
    	
    	foreach ($arr_dict as $row){
    		$arr_display_dict[] = $row;
    		
    		if ($row['did'] == 10){
    			$row_10 = $row;
    			$arr_display_dict_pre = $arr_display_dict;
    			$arr_display_dict = array();
    		}
    	}
    	if (!empty($arr_display_dict_pre))
    		$arr_display_dict_after = $arr_display_dict;
    	
    	// 检测必填字段
    	foreach ($arr_display_dict_pre as $v){
    		$value = $this->getParam("v_" . $v['did']);
    		$key = $cfg['lang'] == 'cn' ? $v['key'] : $v['enkey'];
    		if ($v['ismust'] && empty($value)){
    			exit(sprintf(_T("不能为空"), $key));
    		}
    		// 邮件判断
    		if($v['type'] == 4){
    			if (!ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+",$value))
		    	 	exit(sprintf(_T("邮箱格式不正确"), $key));
    		}
    	}
    	
    	// 判断是否参会
    	$value = $this->getParam("v_10");
    	$data = split("\n", $row_10['data']);
    	if ($value == trim($data[0])){
    		// 检测必填字段
	    	foreach ($arr_display_dict_after as $v){
	    		$value = $this->getParam("v_" . $v['did']);
	    		$key = $cfg['lang'] == 'cn' ? $v['key'] : $v['enkey'];
	    		if ($v['ismust'] && empty($value)){
	    			exit(sprintf(_T("不能为空"), $key));
	    		}
	    	}
    	}
    	
    	// 检测通过，记录数据
    	$content = array();
    	foreach ($arr_dict as $row){
    		$content[$row['did']] = $this->getParam("v_" . $row['did']);
    	}
    	
    	// 插入用户表
    	$values = array();
    	$values['mid'] = $mid;
    	$values['email'] = $mail;
    	$values['content'] = serialize($content);
    	$values['regip'] = $this->getIp(true);
    	$values['regtime'] = date("Y-m-d H:i:s");
    	
    	$this->db->insert("user", $values);
    	
    	// 修改已注册
    	$this->db->update("email",  array("isreg" => 1), $this->db->quoteInto("mid=? and email='?'", array($mid, $mail)));
    	
    	echo _T("注册成功");
    }
}